package com.onlyphoto.onlymall.widget;

import android.content.Context;
import android.os.Build;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.core.content.ContextCompat;

import com.bumptech.glide.Glide;
import com.onlyphoto.onlymall.R;
import com.onlyphoto.onlymall.utils.ConvertUtil;
import com.onlyphoto.onlymall.utils.NetWorkStatus;
import com.onlyphoto.onlymall.utils.ViewUtil;

/**
 * Created by gaochujia on 2019-6-27.
 */

public class CommonStatusView extends LinearLayout {
    private ImageView ivLogo;
    private TextView tvStatusContent;
    private TextView tvNextStep;
    private LinearLayout llContent;

    public CommonStatusView(Context context) {
        this(context, null);
    }

    public CommonStatusView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CommonStatusView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.customer_error, this, true);
        initViews();
    }

    public void initViews() {
        ivLogo = findViewById(R.id.iv_logo);
        tvStatusContent = findViewById(R.id.tv_status_content);
        tvNextStep = findViewById(R.id.tv_next_step);
        llContent = findViewById(R.id.ll_content);
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    public void setContent(int logo, String content, String nextString, boolean... isNetworkError) {
        Glide.with(this.getContext()).load(logo).into(ivLogo);
        tvStatusContent.setText(TextUtils.isEmpty(content) ? "" : content);
        if (TextUtils.isEmpty(nextString)) {
            tvNextStep.setVisibility(GONE);
        } else {
            tvNextStep.setVisibility(VISIBLE);
            tvNextStep.setText(TextUtils.isEmpty(nextString) ? "" : nextString);
        }
        if (isNetworkError != null && isNetworkError.length > 0) {
            if (isNetworkError[0]) {
                tvNextStep.setTextColor(ContextCompat.getColor(getContext(), R.color.theme_color));
                tvNextStep.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.btn_network_bg));
            } else {
                tvNextStep.setTextColor(ContextCompat.getColor(getContext(), R.color.white));
                tvNextStep.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.btn_confirm_bg));
            }
        } else {
            tvNextStep.setTextColor(ContextCompat.getColor(getContext(), R.color.white));
            tvNextStep.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.btn_confirm_bg));
        }

    }

    public void setBigBtn(){
        LayoutParams params = (LayoutParams) tvNextStep.getLayoutParams();
        params.width = ViewUtil.getScreenWidth(tvNextStep.getContext())- ConvertUtil.dp2px(tvNextStep.getContext(),70);
        tvNextStep.setLayoutParams(params);
    }

    public void setType(NetWorkStatus status) {
        tvNextStep.setTag(status);
        llContent.setTag(status);
    }

}